#ifndef __DICTIONARY_H__
#define __DICTIONARY_H__

#include <iostream>
#include<vector>
#include<string>
#include<set>
#include<map>

class Dictionary{
    class AutoRelease {
       public:
         AutoRelease() { /*cout << "AutoRelease()" << endl;*/ }

        ~AutoRelease() {
            //cout << "~AutoRelease()" << endl;
            // 静态数据成员可以在嵌套类中直接访问
            if (_instance) {
                delete _instance;
                _instance = nullptr;
            }
        }
    };

public:
    static Dictionary * createInstance(){
        _instance = new Dictionary();
        return nullptr;
    }

    static Dictionary* getInstance() {
        if(nullptr == _instance) {
			Dictionary::createInstance();
		}
        return _instance;
    }
    static void destory() {
        if (_instance) {
            delete _instance;
            _instance = nullptr;
        }
    }

    void init(const std::string & EnglishDictPath , const std::string & ChineseDictPath ,
            const std::string & EnglishIndexTablePath , const std::string & ChineseIndexTablePath);

    std::vector<std::pair<std::string,int>> getDict(const std::string & DictPath);

    std::map<std::string,std::set<int>> getIndexTable(const std::string & IndexTablePath);

    std::pair<std::string,int> FindEnglishWord(int line);
    std::pair<std::string,int> FindChineseWord(int line);

    std::set<int> FindEnglishLines(std::string zi);
    std::set<int> FindChineseLines(std::string zi);

private:
    Dictionary(){}
    ~Dictionary(){}
    friend class AutoRelease;
	static AutoRelease _ar;
    static Dictionary* _instance;

private:
    std::vector<std::pair<std::string,int>> _english_dict;
    std::vector<std::pair<std::string,int>> _chinese_dict;

    std::map<std::string,std::set<int>> _english_indexTable;
    std::map<std::string,std::set<int>> _chinese_indexTable;
};

#endif